package com.cjjd.c.feature.home.scan;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;

import com.cjjd.c.EventBusKeys;
import com.cjjd.c.R;
import com.cjjd.c.ShareKeys;
import com.cjjd.c.base.ActionBarActivity;
import com.cjjd.c.bean.AddQrCodePayBean;
import com.cjjd.c.bean.HttpResult;
import com.cjjd.c.bean.ScanBean;
import com.cjjd.c.feature.home.chekin.pay.PayPledgeActivity;
import com.cjjd.c.http.CallbackImpl;
import com.cjjd.c.http.JsonUtils;
import com.cjjd.c.utils.SharePrefUtil;
import com.cjjd.c.utils.ToastUtil;
import com.tencent.mm.opensdk.utils.Log;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Response;

/**
 * ----------------------------------
 * Created by xyd on 2017/9/30.
 * <p/>
 * 描  述：
 * <p/>
 * 修订历史:
 * ----------------------------------
 */

public class ScanInputActivity extends ActionBarActivity {
    @Bind(R.id.et_input_money)
    EditText etInputMoney;

    private ScanBean scanBean;
    private String payMoney;

    @Override
    public Object getTitleString() {
        return "消费支付";
    }

    @Override
    public int getContentView() {
        return R.layout.activity_scan;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        scanBean = (ScanBean) getIntent().getSerializableExtra("scanBean");

    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @OnClick(R.id.btn_next)
    public void onViewClicked() {

        if (scanBean != null) {
            if (checkCanPay()) {
                addQrCodePay(scanBean);
            }
        }

    }

    private boolean checkCanPay() {
        payMoney = etInputMoney.getText().toString().trim();
        if (TextUtils.isEmpty(payMoney)) {
            ToastUtil.showToast("请输入金额");
            return false;
        }

        return true;
    }

    /**
     * 创建二维码支付信息
     *
     * @param scanBean
     */
    private void addQrCodePay(ScanBean scanBean) {
        HashMap<String, Object> h1 = new HashMap<>();
        h1.put("token", SharePrefUtil.getString(mContext, ShareKeys.Token, ""));
        h1.put("sessionId", SharePrefUtil.getString(mContext, ShareKeys.sessionId, ""));
        h1.put("hotelId", scanBean.hotel);
        h1.put("costType", scanBean.costType);
        h1.put("costMoney", payMoney);

        Call<HttpResult<AddQrCodePayBean>> call = service.addQrCodePay(JsonUtils.convertParam("addQrCodePay", h1));
        call.enqueue(new CallbackImpl<AddQrCodePayBean>() {
            @Override
            public void responseSuccess(Call<HttpResult<AddQrCodePayBean>> call, Response<HttpResult<AddQrCodePayBean>> response) {

                String payNo = response.body().getData().payNo;
                Intent intent = new Intent(mContext, PayPledgeActivity.class);
                intent.putExtra("fromPage", PayPledgeActivity.PayScan);
                intent.putExtra("payNo", payNo);
                intent.putExtra("scanPayMoney", payMoney);
                //  2017/9/30 put something
                startActivity(intent);
            }
        });

    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(String event) {
        if (EventBusKeys.hadPayScan.equals(event)) {
            Log.e("xyd", "接收到的信息是" + event);
            finish();
        }

    }
}
